package com.vaadin.tutorial.crm.ui.view.login;

import com.vaadin.flow.component.html.H1;
import com.vaadin.flow.component.login.LoginForm;
import com.vaadin.flow.component.login.LoginI18n;
import com.vaadin.flow.component.orderedlayout.VerticalLayout;
import com.vaadin.flow.i18n.I18NProvider;
import com.vaadin.flow.router.BeforeEnterEvent;
import com.vaadin.flow.router.BeforeEnterObserver;
import com.vaadin.flow.router.PageTitle;
import com.vaadin.flow.router.Route;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

/**
 * 登录界面
 * @Author: fan
 * @DateTime: 2021-01-15 10:55
 **/

@Route("login")
@PageTitle("学生管理系统")
public class LoginView extends VerticalLayout implements BeforeEnterObserver {

    private LoginForm login = new LoginForm();

    public LoginView(){
        addClassName("login-view");
        setSizeFull();
        setAlignItems(Alignment.CENTER);
        setJustifyContentMode(JustifyContentMode.CENTER);

        login.setI18n(createI18n());
        login.setAction("login");

        add(new H1("欢迎登录学生管理系统"), login);
    }

    @Override
    public void beforeEnter(BeforeEnterEvent beforeEnterEvent) {
        // inform the user about an authentication error
        if(beforeEnterEvent.getLocation()
                .getQueryParameters()
                .getParameters()
                .containsKey("error")) {
            login.setError(true);
        }
    }

    //登录界面国际化
    private LoginI18n createI18n() {
        final LoginI18n i18n = LoginI18n.createDefault();

        i18n.setHeader(new LoginI18n.Header());
        i18n.getHeader().setTitle("");
        i18n.getHeader().setDescription("");
        i18n.getForm().setUsername("用户名");
        i18n.getForm().setTitle("");
        i18n.getForm().setSubmit("登陆");
        i18n.getForm().setPassword("密码");
        i18n.getForm().setForgotPassword("忘记密码");
        i18n.getErrorMessage().setTitle("用户名或密码错误！");
        i18n.getErrorMessage()
                .setMessage("请输入正确的用户名和密码！");
        i18n.setAdditionalInformation(
                "欢迎登录学生管理系统！");
        return i18n;
    }
}
